package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.RegAddress;
import com.zzc.fwzpxt.entity.Tag;
import com.zzc.fwzpxt.service.IRegAddressService;
import com.zzc.fwzpxt.service.ITagService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import com.zzc.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-10-15
 */
@Controller
@RequestMapping("/admin/tag")
public class TagController extends BaseController {

    @Autowired
    private ITagService iTagService;

    @GetMapping("/list")
    public String list() {
        return blogPath + "tag/list";
    }


    @ResponseBody
    @PostMapping("/getList")
    public Map<String, Object> getUserList(int pageNumber, int pageSize, String searchText) {
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Tag> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Tag> wrapper = new EntityWrapper<>();
        if (StringUtil.isNotEmpty(searchText)) {
            wrapper.like("name", searchText);
        }
        Page<Tag> tagPage = iTagService.selectPage(page, wrapper);
        result.put("total", tagPage.getTotal());
        result.put("rows", tagPage.getRecords());
        return result;
    }

    /**
     * 添加修改
     * @param tag
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public JsonResult save(Tag tag) {
        if (tag.getId() == null) {
            return iTagService.insert(tag) ? renderSuccess("添加成功") : renderError("添加失败");
        } else {
            return iTagService.updateById(tag) ? renderSuccess("修改成功") : renderError("修改失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public JsonResult delete(@RequestBody List<Tag> tags) {
        return iTagService.deleteTagById(tags) ? renderSuccess("删除成功") : renderError("删除失败");
    }

}
